<?php
function connectDB()
{
    $host = "localhost";
    $user = "alexey";
    $pass = "alex_lazer";
    $link_id = mysql_connect($host, $user, $pass);
    mysql_select_db('fly', $link_id) or die(mysql_error());
    mysql_query("SET NAMES 'utf8'");
    mysql_query("SET CHARACTER SET 'utf8'");
    return $link_id;
}

function init()
{
    session_start();
    if ($_SESSION['mid'] != '') {
        return true;
    } else {
        return false;
    }
}

function startTransaction()
{
    mysql_query("SET AUTOCOMMIT=0");
    mysql_query("BEGIN");
}

function commitTransaction()
{
    mysql_query("COMMIT");
}

function rollbackTransaction()
{
    mysql_query("ROLLBACK");
}

function encryptString($login, $password)
{
    $tmpLogin = hash("tiger192,3", $login);
    $tmpConnector = hash("gost", "theFlyNet");
    $tmpPassword = hash("haval224,3", $password);
    $encryptedString = hash("snefru256", $tmpLogin . $tmpConnector . $tmpPassword);
    return $encryptedString;
}

function loginUser($login, $pass)
{
    connectDB();
    startTransaction();

    $password = encryptString($login, $pass);

    $resultSet = mysql_query("SELECT
                                    users.id
                              FROM
                                    users
                              INNER JOIN usrgroup ON users.id = usrgroup.userid
                              INNER JOIN groups ON usrgroup.groupid = groups.id
                              WHERE
                                        nick = '" . mysql_real_escape_string($login) . "'
                                    AND
                                        password = '" . mysql_real_escape_string($password) . "'
                                    AND
                                        groups.header = 'Moderators'") or die(mysql_error());

    if ($row = mysql_fetch_assoc($resultSet)) {
        commitTransaction();
        session_start();
        $_SESSION['mid'] = $row['id'];
        return true;
    } else {
        return false;
    }

}

function getModeratorsTopics()
{
    connectDB();
    startTransaction();

    $resultSet = mysql_query("SELECT
                                    topics.id,
                                    header,
                                    date,
                                    time
                              FROM
                                    topics
                              INNER JOIN
                                    moderators ON topics.id = moderators.topicid
                              WHERE moderators.userid = '1'
                              ORDER BY date DESC") or die(mysql_error()); // Add user's mid

    if ($resultSet) {
        commitTransaction();
        return $resultSet;
    } else {
        rollbackTransaction();
        return null;
    }
}

function hasNewComments($topicId)
{
    connectDB();
    startTransaction();

    $resultSet = mysql_query("SELECT id FROM tmptopicmessages WHERE topicid = '" . mysql_real_escape_string($topicId) . "' LIMIT 1") or die(mysql_error());

    if ($resultSet) {
        if ($row = mysql_fetch_array($resultSet)) {
            commitTransaction();
            return true;
        } else {
            return false;
        }
    } else {
        rollbackTransaction();
        return false;
    }
}

function getNewTopicComments($topicId)
{
    connectDB();
    startTransaction();

    $resultSet = mysql_query("SELECT
                                    topicmessages.id,
                                    users.nick AS author,
                                    content,
                                    date,
                                    time
                              FROM
                                    topicmessages
                              INNER JOIN
                                    users ON topicmessages.userid = users.id
                              WHERE
                                    topicid = '" . mysql_real_escape_string($topicId) . "'
                                    AND
                                    checked = '0'
                              ORDER BY date, time") or die(mysql_error());

    if ($resultSet) {
        commitTransaction();
        return $resultSet;
    } else {
        rollbackTransaction();
        return null;
    }
}

function getTopicCommentContentById($commentId)
{
    connectDB();
    startTransaction();

    $resultSet = mysql_query("SELECT
                                    content
                              FROM
                                    topicmessages
                              WHERE
                                    id = '" . mysql_real_escape_string($commentId) . "'") or die(mysql_error());

    if ($resultSet) {
        commitTransaction();
        return $resultSet;
    } else {
        rollbackTransaction();
        return null;
    }
}



function getNewCommentImages($commentId)
{
    connectDB();
    startTransaction();

    $resultSet = mysql_query("SELECT
                                    id,
                                    path
                              FROM
                                    tmptopicimages
                              WHERE
                                    topicmsgid = '" . mysql_real_escape_string($commentId) . "'") or die(mysql_error());

    if ($resultSet) {
        commitTransaction();
        return $resultSet;
    } else {
        rollbackTransaction();
        return null;
    }
}

function redirect($url)
{
    echo "<script language='JavaScript'>
        location.href = '" . $url . "'
    </script>";
//    header("Location: " . $url);
}

function countNewMessages($topicId)
{
    connectDB();
    startTransaction();

    $resultSet = mysql_query("SELECT count(id) AS msgs FROM topicmessages WHERE     topicid = '" . mysql_real_escape_string($topicId) . "'
                                                                                AND
                                                                                    checked = '0'");

    if ($resultSet) {
        commitTransaction();
        if ($row = mysql_fetch_assoc($resultSet)) {
            return $row['msgs'];
        } else return 0;
    } else {
        rollbackTransaction();
        return 0;
    }
}
